using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace System.Utility
{
    public static class Extensions
    {
        public static string Format(this DateTime dateTime)
        {
            return dateTime.Format("yyyy-MM-dd HH:mm:ss");
        }

        public static string Format(this Nullable<DateTime> dateTime)
        {
            return dateTime.Format("yyyy-MM-dd HH:mm:ss");
        }

        public static string Format(this DateTime dateTime, string format)
        {
            return dateTime.ToString(format);
        }

        public static string Format(this Nullable<DateTime> dateTime, string format)
        {
            return dateTime.HasValue ? dateTime.Value.Format(format) : string.Empty;
        }

        public static bool Include(this string content, string value)
        {
            return content.Include(value, StringComparison.Ordinal);
        }

        public static bool Include(this string content, string value, StringComparison comparison)
        {
            if (content == null)
            {
                throw new ArgumentNullException("content");
            }
            if (string.IsNullOrEmpty(value))
            {
                return false;
            }
            return content.IndexOf(value, comparison) >= 0;
        }

        public static bool IncludeIc(this string content, string value)
        {
            return content.Include(value, StringComparison.OrdinalIgnoreCase);
        }

        public static bool IsMatch(this string content, string pattern)
        {
            return content.IsMatch(pattern, RegexOptions.None);
        }

        public static bool IsMatch(this string content, string pattern, RegexOptions options)
        {
            if (content == null)
            {
                throw new ArgumentNullException("content");
            }
            return Regex.IsMatch(content, pattern, options);
        }

        public static string Join(this IEnumerable<string> enumerable, string spliter)
        {
            if (enumerable == null)
            {
                throw new ArgumentNullException("enumerable");
            }
            StringBuilder builder = new StringBuilder();
            using (IEnumerator<string> enumerator = enumerable.GetEnumerator())
            {
                if (enumerator.MoveNext())
                {
                    builder.Append(enumerator.Current);
                }
                while (enumerator.MoveNext())
                {
                    builder.Append(spliter).Append(enumerator.Current);
                }
            }
            return builder.ToString();
        }

        public static Match Match(this string content, string pattern)
        {
            return content.Match(pattern, RegexOptions.None);
        }

        public static Match Match(this string content, string pattern, RegexOptions options)
        {
            if (content == null)
            {
                throw new ArgumentNullException("content");
            }
            return Regex.Match(content, pattern, options);
        }
    }
}